//
//  ProgressView.m
//  News
//
//  Created by 航汇聚科技 on 2018/4/4.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#define kColorWithRGB(r, g, b, a) [UIColor colorWithRed:(r) / 255.f green:(g) / 255.f blue:(b) / 255.f alpha:a]
#define kWindow [UIApplication sharedApplication].keyWindow

#import "ProgressView.h"

@interface ProgressView ()
{
    float _lineWidth;
    
    CAShapeLayer *_progressLayer;
    
}
@property (nonatomic, strong) UIImageView *endPoint;


@end

@implementation ProgressView


+ (instancetype)sharedProgressView {
    static ProgressView *progress = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        progress = (ProgressView *)[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] firstObject];
        
        progress.frame = [UIScreen mainScreen].bounds;
        
        progress.backgroundColor = kColorWithRGB(0, 0, 0, 0.5);
        
        [progress buildLayout];
    });
    
    return progress;
}

- (void)buildLayout {
    
    _lineWidth = 0.1*([UIScreen mainScreen].bounds.size.width - 30);
    
    float centerX = ([UIScreen mainScreen].bounds.size.width - 30)/2.0;
    float centerY = ([UIScreen mainScreen].bounds.size.height - 100)/2.0;
    //半径
    float radius = ([UIScreen mainScreen].bounds.size.width - _lineWidth)/2.0;
    
    //创建贝塞尔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(-0.5f*M_PI) endAngle:1.5f*M_PI clockwise:YES];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = self.bounds;
    layer.fillColor = [[UIColor clearColor] CGColor];
    layer.strokeColor = [kColorWithRGB(50, 50, 50, 1) CGColor];
    
    layer.lineWidth = _lineWidth;
    layer.path = path.CGPath;
    layer.strokeEnd = 1;
    
    [self.layer addSublayer:layer];
    
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor = [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor = [[UIColor blackColor] CGColor];
    _progressLayer.lineCap = kCALineCapRound;
    _progressLayer.lineWidth = _lineWidth;
    _progressLayer.path = [path CGPath];
    _progressLayer.strokeEnd = 0;
    
    //设置渐变颜色
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)[kColorWithRGB(255, 151, 0, 1) CGColor],(id)[kColorWithRGB(255, 203, 0, 1) CGColor], nil]];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    [gradientLayer setMask:_progressLayer]; //用progressLayer来截取渐变层
    [self.layer addSublayer:gradientLayer];
    
    //用于显示结束位置的小点
    _endPoint = [[UIImageView alloc] init];
    _endPoint.frame = CGRectMake(0, 0, _lineWidth - endPointMargin*2,_lineWidth - endPointMargin*2);
    _endPoint.hidden = true;
    _endPoint.backgroundColor = [UIColor blackColor];
    _endPoint.image = [UIImage imageNamed:@"endPoint"];
    _endPoint.layer.masksToBounds = true;
    _endPoint.layer.cornerRadius = _endPoint.bounds.size.width/2;
    [self addSubview:_endPoint];
}
static CGFloat endPointMargin = 1.0f;
//更新小点的位置
-(void)updateEndPoint
{
    //转成弧度
    CGFloat angle = M_PI*2*_progress;
    float radius = ([UIScreen mainScreen].bounds.size.width-_lineWidth)/2.0;
    int index = (angle)/M_PI_2;//用户区分在第几象限内
    float needAngle = angle - index*M_PI_2;//用于计算正弦/余弦的角度
    float x = 0,y = 0;//用于保存_dotView的frame
    switch (index) {
        case 0:
            NSLog(@"第一象限");
            x = radius + sinf(needAngle)*radius;
            y = radius - cosf(needAngle)*radius;
            break;
        case 1:
            NSLog(@"第二象限");
            x = radius + cosf(needAngle)*radius;
            y = radius + sinf(needAngle)*radius;
            break;
        case 2:
            NSLog(@"第三象限");
            x = radius - sinf(needAngle)*radius;
            y = radius + cosf(needAngle)*radius;
            break;
        case 3:
            NSLog(@"第四象限");
            x = radius - cosf(needAngle)*radius;
            y = radius - sinf(needAngle)*radius;
            break;
            
        default:
            break;
    }
    
    //更新圆环的frame
    CGRect rect = _endPoint.frame;
    rect.origin.x = x + endPointMargin;
    rect.origin.y = y + endPointMargin;
    _endPoint.frame = rect;
    //移动到最前
    [self bringSubviewToFront:_endPoint];
    _endPoint.hidden = false;
    if (_progress == 0 || _progress == 1) {
        _endPoint.hidden = true;
    }
}

- (void)show {
    dispatch_async(dispatch_get_main_queue(), ^{
        [kWindow addSubview:self];
    });
}

- (void)hide {
    [self removeFromSuperview];
}

#pragma mark - setter
- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self addGestureRecognizer:tap];
    
    [self updateEndPoint];
    _progressLayer.strokeEnd = progress;
    [_progressLayer removeAllAnimations];
}


@end

